package com.sctvcloud.qingchuan.ui.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.util.SparseArray;
import android.view.ViewGroup;

import com.ruihang.generalibrary.utils.ListUtils;
import com.sctvcloud.qingchuan.beans.PoliticsAccountTag;
import com.sctvcloud.qingchuan.ui.activities.PoliticsChooseObjActivity;
import com.sctvcloud.qingchuan.ui.fragment.ColumnBaseFragment;
import com.sctvcloud.qingchuan.ui.fragment.PoliticsAccountFragment;

import java.lang.ref.WeakReference;
import java.util.List;

/**
 * 选择问政对象
 */
public class PoliticsTagPagerAdapter extends FragmentStatePagerAdapter {

    private List<PoliticsAccountTag> list;
    private SparseArray<WeakReference<ColumnBaseFragment>> fragmentWeaks;

    private int topHeight;
    private long authorId;

    private PoliticsChooseObjActivity parentActivity;


    public PoliticsTagPagerAdapter(PoliticsChooseObjActivity parentActivity, FragmentManager fm, List<PoliticsAccountTag> list) {
        super(fm);
        this.list = list;
        this.parentActivity = parentActivity;
        fragmentWeaks = new SparseArray<>();
    }

    @Override
    public Fragment getItem(int position) {
        if (ListUtils.isListValued(list) && position < list.size()) {
            ColumnBaseFragment fragment = new PoliticsAccountFragment();
            ((PoliticsAccountFragment) fragment).setTagId(list.get(position).getAuthorTagId());
            ((PoliticsAccountFragment) fragment).setParentActivity(parentActivity);
            fragment.setIndexInViewPager(position);
            fragmentWeaks.put(position, new WeakReference<ColumnBaseFragment>(fragment));
            return fragment;
        } else {
            return null;
        }
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return list.get(position).getAuthorTagName();
    }

    public void setData(List<PoliticsAccountTag> data) {
        this.list = data;
        notifyDataSetChanged();
    }

    public ColumnBaseFragment getFragmentByPos(int pos) {
        WeakReference<ColumnBaseFragment> wrf = fragmentWeaks.get(pos);
        if (wrf != null) {
            return wrf.get();
        }
        return null;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        super.destroyItem(container, position, object);
    }

    public int getPosCurrentPage(String type) {
        if (getCount() == 0) {
            return -1;
        }
        for (int i = 0; i < getCount(); i++) {
            if (list.get(i).getAuthorTagId().equals(type)) {
                return i;
            }
        }
        return -1;
    }

    public void setTopHeight(int topHeight) {
        this.topHeight = topHeight;
    }

    @Override
    public int getItemPosition(Object object) {
        return PagerAdapter.POSITION_NONE;
    }

    /**
     * 刷新页面中加载的fragment中的选中项
     *
     * @param authorId
     */
    public void setLastSelectedId(long authorId) {
        this.authorId = authorId;
    }

}
